#!/bin/bash

base=$(cd $(dirname $0);pwd)
script_home=${base%/script*}
config=${script_home%/*}/config.yml


if [ $(grep \; ${config} | grep base | wc -l) -gt 0 -o \
     $(grep \; ${config} | grep lowcode | wc -l) -gt 0 -o \
     $(grep \; ${config} | grep node | wc -l) -gt 0 -o \
     $(grep \; ${config} | grep cmdb | wc -l) -gt 0 -o \
     $(grep \; ${config} | grep platform | wc -l) -gt 0 -o \
     $(grep \; ${config} | grep apm | wc -l) -gt 0 ]; then
    # 判断这些基础服务是否被定义
    if [ $(grep \; ${config} | grep nginx | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep nacos | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep minio | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep redis | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep zookeeper | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep kafka | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep elasticsearch | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep hadoop | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep clickhouse | wc -l) -eq 0 -o \
         $(grep \; ${config} | grep mysql | wc -l) -eq 0 ]; then
        echo "缺少必备组件，请检查${config}"
        exit 0
    fi
fi


list=$(grep -E "^- " $config |awk '{print $2}'>$base/.cache/install_info)
for line in `cat $base/.cache/install_info`
do
    check_minio=$(echo $line |grep minio |grep clickhouse |wc -l)
    if [ $check_minio -gt 0 ];then
        echo "不能安装在同一主机 [minio, clickhouse],请检查${config}"
	exit 1
    fi
done

